package util

import (
	"api-go/serializer"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"strings"
	"time"

	uuid "github.com/satori/go.uuid"
)

//通用GET请求第三方链接，返回结果为请求的结果和错误数据
func Get(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	res, err := http.NewRequest("GET", url, nil)
	res.Header.Add("X-Forwarded-For", RandIp())
	if err != nil {
		fmt.Println("1", err)
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(res)
	if err != nil {
		fmt.Println("2", err)
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("3", err)
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(string(data))
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		fmt.Println("4", err)
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	return bodyMapData, e
}

//wyy专用get请求，因为需要referer头
func WyyGet(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	// resp, err := http.Get(url)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Set("referer", "https://music.163.com/")
	req.Header.Set("cookie", "MUSIC_U=582a3c6c3351f863107be889915d2f87bfe6214f7ac2583e40bdfd9e84a2d617993166e004087dd3d78b6050a17a35e705925a4e6992f61dfe3f0151024f9e31")
	req.Header.Add("X-Forwarded-For", RandIp())
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(req)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	return bodyMapData, e
}

//用于返回单string类型，不用转json的
func SGet(url string) (interface{}, *serializer.Response) {
	var e *serializer.Response
	resp, err := http.Get(url)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// //赋值用于后面将string转为json
	// bodyMapData := map[string]interface{}{}
	// err = json.Unmarshal(data, &bodyMapData)
	// if err != nil {
	// 	e = serializer.ErrorResponse(serializer.CodeUnknownError)
	// }
	return string(data), e
}

//QQ专用get请求，因为需要referer头
func QQGet(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	// resp, err := http.Get(url)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Set("referer", "https://y.qq.com/")
	req.Header.Set("content-type", "text/plain;charset=UTF-8")
	req.Header.Set("User-Agent", "Mozilla/5.0 (iPad; CPU OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1")
	req.Header.Add("X-Forwarded-For", RandIp())
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(req)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	return bodyMapData, e
}

//kuwo专用get请求，因为需要referer头
func KuwoGet(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	// resp, err := http.Get(url)
	u1 := uuid.NewV4().String()
	url = url + "&reqId=" + u1
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Set("Referer", "https://kuwo.cn/")
	req.Header.Set("csrf", "95MWTYC4FP")
	req.Header.Set("Cookie", "kw_token=95MWTYC4FP")
	req.Header.Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
	req.Header.Add("userIP", RandIp())
	req.Header.Add("Host", "kuwo.cn")
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(req)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(resp)
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(data)
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(bodyMapData)
	return bodyMapData, e
}

//kugou专用get请求，因为需要referer头
func KugouGet(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	// resp, err := http.Get(url)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Set("origin", "https://www.kugou.com/")
	req.Header.Set("referer", "https://www.kugou.com/")
	req.Header.Set("Cookie", "kg_mid=a3d93ffdeca72d45ad5935026a6dcfa9")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Mobile Safari/537.36")
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(req)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(resp)
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(data)
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(bodyMapData)
	return bodyMapData, e
}

//用于kugou对字符串含有特殊字符导致转化不成json
func KugouGetS(url string) (interface{}, *serializer.Response) {
	var e *serializer.Response
	resp, err := http.Get(url)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	//由于接口有"<!--KG_TAG_RES_START-->"特殊字符串，导致无法转Json，这里把这个字符串去掉
	newData := strings.Replace(string(data), "<!--KG_TAG_RES_START-->", "", 1)
	newData = strings.Replace(newData, "<!--KG_TAG_RES_END-->", "", 1)
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal([]byte(newData), &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	return bodyMapData, e
}

//migu专用get请求，因为需要referer头
func MiguGet(url string) (interface{}, *serializer.Response) {
	client := &http.Client{}
	var e *serializer.Response
	// resp, err := http.Get(url)
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Set("origin", "https://m.music.migu.cn/")
	req.Header.Set("referer", "https://m.music.migu.cn/")
	req.Header.Add("X-Forwarded-For", RandIp())
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	resp, err := client.Do(req)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(resp)
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(data)
	//赋值用于后面将string转为json
	bodyMapData := map[string]interface{}{}
	err = json.Unmarshal(data, &bodyMapData)
	if err != nil {
		e = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	// fmt.Println(bodyMapData)
	return bodyMapData, e
}

//随机生成Ip，防止被封
func RandIp() string {
	rand.Seed(time.Now().Unix())
	ip := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
	return ip
}
